<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Notification extends ORM {
    
    protected $_belongs_to = array(
                'user'    => array('model' => 'user', 'foreign_key' => 'user_id'),
                
    );
    
    /**
     * Creates a new controller instance. Each controller must be constructed
     * with the request object that created it.
     *
     * @param   int  $user_id  user id
     * @param   int  $amount   Amount of returned records
     * @return  array          keys => user,title,date,image
     */
    
    public function get_notifications($user_id, $amount = null, $all = true)
    {
                                              
        $this->select('user_profiles.name', 
                      'user_profiles.surname', 
                      'user_profiles.description', 
                      'user_profiles.url',
                      array('user_profiles.file_id','photo'),
                      array('files.id','file_id'),
                      array('files.name','file_name'),
                      array('files.extension','file_extension'))
             ->join('user_profiles')->on('notifications.user_id', '=', 'user_profiles.user_id')
             ->join('files','LEFT')->on('user_profiles.file_id', '=', 'files.id')
             
             ->where('resource_owner_id', '=', $user_id)
             ->where('notifications.user_id', '!=', $user_id)
            
             ->order_by('id', 'desc');
        
        if($all)
        {
             $my_contacts = DB::select(array(DB::expr('CASE sender_id WHEN '.$user_id.' THEN recipient_id else sender_id END'),'id'))
                           ->from('contacts')
                           ->where('status', '=', 1)
                           ->and_where_open()
                           ->where('sender_id','=', $user_id)
                           ->or_where('recipient_id','=', $user_id)
                           ->and_where_close();
             
             $this->or_where('notifications.user_id', 'IN', $my_contacts);
        }
        
        if ($amount !== null)
        {
            $this->limit($amount);
        }
        
        $raw_notifications = $this->find_all();
          
        $notifications = array();

        foreach($raw_notifications as $raw_notification)
        {
            $notification_data = unserialize($raw_notification->event_params);
            $title = Kohana::message('notification', $raw_notification->event_type.'.title');
            $description = Kohana::message('notification', $raw_notification->event_type.'.description');
            
            $data = array('id'               => $raw_notification->id,
                          'user_name'        => $raw_notification->name,
                          'user_surname'     => $raw_notification->surname,
                          'user_description' => $raw_notification->description,
                          'user_url'         => $raw_notification->url,
                          'event_type'       => $raw_notification->event_type,
                          'user_photo'       => Media::get_file_path($raw_notification->file_name, 
                                                        $raw_notification->file_id, 
                                                        $raw_notification->file_extension, 
                                                        true, 
                                                        'mini_square', 
                                                        false),
                          'title'           => __($title, $notification_data),
                          'date'            => $raw_notification->date);

            array_push($notifications, $data);

        } 
        
        return $notifications;
    }
         
         
}